package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.enums.AddressStatusEnum;
import org.example.enums.BizCodeEnum;
import org.example.exception.BizException;
import org.example.interceptor.LoginInterceptor;
import org.example.mapper.AddressMapper;
import org.example.model.AddressDO;
import org.example.request.AddressAddRequest;
import org.example.service.AddressService;
import org.example.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 根据id查询地址
     */
    public AddressVO findById(Long id) {
        Long userId = LoginInterceptor.threadLocal.get().getId();
        QueryWrapper<AddressDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id).eq("user_id", userId);
        AddressDO addressDO = addressMapper.selectOne(queryWrapper);
        if (addressDO==null){
            throw new BizException(BizCodeEnum.ADDRESS_NO_EXITS);
        }
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(addressDO, addressVO);
        return addressVO;
    }

    /**
     * 新增地址
     */
    @Override
    public void add(AddressAddRequest addressAddRequest) {
        Long userId = LoginInterceptor.threadLocal.get().getId();
        AddressDO addressDO = new AddressDO();
        //1、设置addressAddRequest没有的属性
        addressDO.setUserId(userId);
        addressDO.setCreateTime(new Date());
        //2、赋值属性
        BeanUtils.copyProperties(addressAddRequest, addressDO);
        //3、判断是否为默认地址
        if (addressDO.getDefaultStatus() == AddressStatusEnum.DEFAULT_ADDRESS.getStatus()) {
            //查看数据库中是否有默认地址
            AddressDO defaultAddressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>()
                    .eq("user_id", userId)
                    .eq("default_status", AddressStatusEnum.DEFAULT_ADDRESS.getStatus()));
            //如果存在默认地址，就把它设为0
            if (defaultAddressDO != null) {
                defaultAddressDO.setDefaultStatus(AddressStatusEnum.COMMON_ADDRESS.getStatus());
                //根据id修改
                addressMapper.update(defaultAddressDO, new QueryWrapper<AddressDO>()
                        .eq("id", defaultAddressDO.getId()));
            }
        }
        //4、存入数据库
        int rows = addressMapper.insert(addressDO);
        if (rows != 1) {
            throw new BizException(BizCodeEnum.ADDRESS_ADD_ERROR);
        }
    }

    /**
     * 根据id删除地址
     */
    @Override
    public int deleteById(Long addressId) {
        Long userId = LoginInterceptor.threadLocal.get().getId();
        int rows = addressMapper.delete(new QueryWrapper<AddressDO>().eq("id", addressId)
                .eq("user_id", userId));
        return rows;
    }

    /**
     * 查询用户的所有地址
     */
    @Override
    public List<AddressVO> list() {
        Long userId = LoginInterceptor.threadLocal.get().getId();
        List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", userId));
        //使用JDK8新特性进行遍历
        List<AddressVO> addressVOList = addressDOList.stream().map(obj -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(obj, addressVO);
            return addressVO;
        }).collect(Collectors.toList());
        return addressVOList;
    }
}
